package com.zf.yichat.controller.message;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.UltragroupAdminUserDto;
import com.zf.yichat.dto.UltragroupMemberUserDto;
import com.zf.yichat.dto.UserJoinUltragroupDto;
import com.zf.yichat.mapper.UserJoinUltragroupApplyMapper;
import com.zf.yichat.model.MessageUltragroup;
import com.zf.yichat.service.config.UltragroupService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.common.FsConst;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 超级群
 */
@Controller
@RequestMapping("ultragroup")
@Slf4j
public class MessageUltragroupController extends BaseController {

    @Autowired
    private UltragroupService ultragroupService;

    @Resource
    private UserJoinUltragroupApplyMapper userJoinUltragroupApplyMapper;

    /**************超级群页面开始*******************************/
    /**
     * 超级群列表
     * @return
     */
    @RequestMapping("/index")
    public String ultragroupIndex() {
        return "ultragroup/index";
    }


    /**
     * 超级群申请列表
     * @return
     */
    @RequestMapping("/apply")
    public String ultragroupApply() {
        return "ultragroup/apply";
    }

    /**
     * 超级群申请列表-审核操作
     * @return
     */
    @RequestMapping("/applying")
    public String ultragroupApplying(String userId, String ultragroupId) {
        getRequest().setAttribute("userId", userId);
        getRequest().setAttribute("ultragroupId", ultragroupId);
        return "ultragroup/applying";
    }

    /**
     * 超级群管理员列表
     * @return
     */
    @RequestMapping("/ultragroupAdmin")
    public String ultragroupAdmin(String ultragroupId) {
        getRequest().setAttribute("ultragroupId", ultragroupId);
        return "ultragroup/ultragroupAdmin";
    }

    /**
     * 超级群成员列表
     * @return
     */
    @RequestMapping("/ultragroupMember")
    public String ultragroupMember(String ultragroupId) {
        getRequest().setAttribute("ultragroupId", ultragroupId);
        return "ultragroup/ultragroupMember";
    }
    /**************超级群页面结束*******************************/


    /**
     * 创建超级群
     * @param userId
     * @param groupName
     * @return
     */
    @RequestMapping("/createUltragroup")
    @ResponseBody
    public FsResponse createUltragroup(Long userId, String groupName, String avatar, String approval) {
        try {
            if (StringUtils.isNotBlank(groupName) && null != userId) {
                Integer code = ultragroupService.createUltragroup(userId, groupName, avatar, approval);
                if (FsConst.Status.IS_OK == code) {
                    return FsResponseGen.successData(code);
                }
            }
        } catch (Exception e) {
            log.info("创建超级群失败：", e.getMessage());
        }
        return FsResponseGen.failMsg("创建失败!");
    }

    /**
     * 获取创建的超级群
     * @return
     */
    @RequestMapping("/queryList")
    @ResponseBody
    public FsResponse ultragroupQuery() {
        try {
            List<MessageUltragroup> ultragroupList = ultragroupService.getUltragroupList();
            return FsResponseGen.successData(ultragroupList);
        } catch (Exception e) {
            log.info("获取超级群列表失败：", e.getMessage());
        }
        return FsResponseGen.gen(false);
    }


    /**
     * 编辑超级群信息
     * @param ultragroupId
     * @param status
     * @param ultragroupName
     * @return
     */
    @RequestMapping("/updateUltragroup")
    @ResponseBody
    public FsResponse updateUltragroup(Long ultragroupId, String status, String ultragroupName, String avatar, String approval) {

        try {
            ultragroupService.updateUltragroup(ultragroupId, status, ultragroupName, avatar, approval);
            return FsResponseGen.success();
        } catch (Exception e) {
            log.error("更新超级群失败！", e.getMessage());
            e.printStackTrace();
            return FsResponseGen.failMsg(e.getMessage());
        }
    }

    /**
     * 删除超级群信息
     * @param ultragroupId
     * @param id
     * @return
     */
    @RequestMapping("/deleteUltragroup")
    @ResponseBody
    public FsResponse deleteUltragroup(Long id, String ultragroupId) {

        try {
            ultragroupService.deleteUltragroup(id, ultragroupId);
            return FsResponseGen.success();
        } catch (Exception e) {
            log.error("删除超级群失败！", e.getMessage());
            e.printStackTrace();
            return FsResponseGen.failMsg(e.getMessage());
        }
    }

    /**
     * 创建聊天室管理员
     * @param ultragroupId
     * @param userIds
     * @return
     */
    @RequestMapping("/createUltragroupAdmin")
    @ResponseBody
    public FsResponse createUltragroupAdmin(String ultragroupId, String userIds) {
        try {
            if (StringUtils.isNotBlank(ultragroupId) && StringUtils.isNotBlank(userIds)) {
                Integer code = ultragroupService.UltragroupAdmin(ultragroupId, userIds);
                if (FsConst.Status.IS_OK == code) {
                    return FsResponseGen.gen(true);
                }
            }
        } catch (Exception e) {
            log.info("创建聊天室管理员失败：", e.getMessage());
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 查询超级群管理员
     * @param ultragroupId
     * @return
     */
    @RequestMapping("/ultragroupAdminList")
    @ResponseBody
    public FsResponse queryUltragroupAdmin(Integer page, Integer limit,String ultragroupId, String userId, String nick, String mobile, String realName) {

        if (StringUtils.isBlank(ultragroupId)) {
            return FsResponseGen.failMsg("参数错误[ultragroupId]");
        }

        PageHelper.startPage(page, limit);
        List<UltragroupAdminUserDto> list = ultragroupService.queryUltragroupAdmin(ultragroupId, userId, nick, mobile, realName);
        return DtoChangeUtils.getPageList(list, data -> {
            UltragroupAdminUserDto adminUserDto = new UltragroupAdminUserDto();
            BeanUtils.copyProperties(data, adminUserDto);
            return adminUserDto;
        });
    }

    /**
     * 删除超级群管理员
     * @param ultragroupId
     * @param userId
     * @return
     */
    @RequestMapping("/deleteUltragroupAdmin")
    @ResponseBody
    public FsResponse deleteChatroomAdmin(String ultragroupId, String userId) throws Exception {

        if (StringUtils.isBlank(ultragroupId)) {
            return FsResponseGen.failMsg("参数错误[ultragroupId]");
        }
        if (StringUtils.isBlank(userId)) {
            return FsResponseGen.failMsg("参数错误[userId]");
        }

        return FsResponseGen.successData(ultragroupService.deleteUltragroupAdmin(ultragroupId, userId));
    }


    /**
     * 查询超级群成员列表
     * @param ultragroupId
     * @return
     */
    @RequestMapping("/ultragroupMemberList")
    @ResponseBody
    public FsResponse queryUltragroupMemberList(Integer page, Integer limit,String ultragroupId, String userId, String nick, String mobile, String realName) {

        if (StringUtils.isBlank(ultragroupId)) {
            return FsResponseGen.failMsg("参数错误[ultragroupId]");
        }

        PageHelper.startPage(page, limit);
        List<UltragroupMemberUserDto> list = ultragroupService.queryUltragroupMember(ultragroupId, userId, nick, mobile, realName);
        return DtoChangeUtils.getPageList(list, data -> {
            UltragroupMemberUserDto adminUserDto = new UltragroupMemberUserDto();
            BeanUtils.copyProperties(data, adminUserDto);
            return adminUserDto;
        });
    }


    /**
     * 删除超级群成员
     * @param ultragroupId
     * @param userId
     * @return
     */
    @RequestMapping("/deleteUltragroupMember")
    @ResponseBody
    public FsResponse deleteUltragroupMember(String ultragroupId,String userId) throws Exception {

        if (StringUtils.isBlank(ultragroupId)) {
            return FsResponseGen.failMsg("参数错误[ultragroupId]");
        }
        if (StringUtils.isBlank(userId)) {
            return FsResponseGen.failMsg("参数错误[userId]");
        }

        return FsResponseGen.successData(ultragroupService.deleteUltragroupMember(ultragroupId, userId));
    }


    /**
     * 用户申请进超级群列表
     * @return
     */
    @RequestMapping("/userApplyList")
    @ResponseBody
    public FsResponse userApplyList(Integer page, Integer limit) {

        PageHelper.startPage(page, limit);
        List<UserJoinUltragroupDto> list = userJoinUltragroupApplyMapper.selectList();
        return DtoChangeUtils.getPageList(list, record -> {
            UserJoinUltragroupDto ultragroupDto = new UserJoinUltragroupDto();
            BeanUtils.copyProperties(record, ultragroupDto);
            return ultragroupDto;
        });
    }



    /**
     * 审核申请入超级群成员
     * @return
     */
    @RequestMapping("/applyed")
    @ResponseBody
    public FsResponse ultragroupApplyed(String ultragroupId, String userId, String status) {
        try {
            ultragroupService.ultragroupApply(ultragroupId, userId, status);
            return FsResponseGen.success();
        } catch (Exception e) {
            log.info("审核申请入超级群成员失败：", e.getMessage());
            return FsResponseGen.failMsg("审核申请入超级群成员失败");
        }
    }
}
